/**
 * Created by smp on 2017/4/24.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/majorHazard/assessmentReport/';
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=parent.layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['1150px', '70%'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
var getStyleAfterCheckDate = function (date) {
    var now = new Date();
    date = new Date(date);
    var oneMonthAgo = new Date(date.valueOf() - 30 * 24 * 60 * 60 * 1000);
    if (date < now){
        return 'background-color:#ff4d4d;';
    }else if (oneMonthAgo < now ){
        return 'background-color:#fff400;';
    }
};
/**
 * 打开详情页面
 */
function openDetailPage(id){
    WindowManager.openLayer({
        type: 2,
        title: '登记信息',
        area: ['1150px', '70%'],
        content: modelPath + 'detail.shtml?id='+id
    });
}
$(function () {
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/majorHazard/assessmentReport/list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        // idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect:true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 80, checkbox: true},
            {field: 'reportName', title: '报告名称', width: 100,formatter:function(value,row,index){
                return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
            },  sortable: true},
            {field: 'evaluationConclusion', title: '评价(评估)结论', width: 80,  sortable: true},
            {field: 'evaluationUnit', title: '评价单位', width: 80, sortable: true},
            {field: 'endTime', title: '有效截止日期', width: 60, styler: function (value, row) {
                return getStyleAfterCheckDate(value);
            }, sortable: true},
            {field: 'facilitiesAndParts', title: '关键装置要害部位', width: 100 },
            {field: 'level', title: '级别', width: 100 },
            {field: 'contractorName', title: '承包人', width: 100 },
        /*    {field: 'addTime', title: '附件', width: 100 },*/
            {field: 'addTime', title: '登记时间', width: 100 }
        ]],
        toolbar: '#toolbar'
    });

    //添加按钮事件
    $('#majorHazard_assessment_add').on('click',function () {

        var title='添加登记信息';
        var content='add.shtml?resourceId=' + $.getQueryParam('resourceId');
        openLayer(title,content);
    });
    //编辑按钮事件
    $('#majorHazard_assessment_edit').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        var title='编辑登记信息';
        var content='edit.shtml?id='+id+'&resourceId=' + $.getQueryParam('resourceId');
        openLayer(title,content);
    });
    //删除按钮事件
    $('#majorHazard_assessment_delete').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要删除的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: modelPath+'delete.shtml',
                data: {id:id,delState:1},
                dataType: "json",
                success: function(result){
                    if (result.success) {
                        $('#dg1').datagrid('clearSelections').datagrid('reload');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });
    /**
     * 高级查询列表
     */
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    /**
     * 查询按钮
     */
    $('#selectAssessmentReport').on('click',function(){
        var reportName=$('#reportName').textbox("getValue");
        var code=$('#code').textbox("getValue");
        var evaluationTime=$('#evaluationTime').combobox("getValue");
        var evaluationUnit=$('#evaluationUnit').textbox("getValue");
        var beginTime=$('#beginTime').datebox("getValue");
        var endTime=$('#endTime').datebox("getValue");
        var level=$('#level').textbox("getValue");
        $('#dg1').datagrid('load', {reportName:reportName,code:code,evaluationTime:evaluationTime,evaluationUnit:evaluationUnit,beginTime:beginTime,endTime:endTime,level:level,sort: 'addTime', order: 'desc'});
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
    });
});